#include "my.h"
using namespace std;
void XayDungNen();
void ThongTinUngDung();
void NhapThongTin();
void XoaManHinhThongTinUngDung();
void gotoxy(int x,int y);
void color(int x);
void ThietLapCacKieu(int n);
void ThoiGian5s();
void ThoiGianThat();
void GioiThieu();
void main()
{
	GioiThieu();
	XoaManHinhThongTinUngDung();
	gotoxy(0,0);
	XayDungNen();
	ThongTinUngDung();
	ThoiGianThat();
	NhapThongTin();
	XoaManHinhThongTinUngDung();
	int n = 7;
	color(525);
	gotoxy(20,6);
	printf("Cho khoang 5 giay ban se thay duoc hieu ung cua ma tran");
	ThoiGian5s();	
	ThietLapCacKieu(n);
	gotoxy(0,0);
	getch();

}
void GioiThieu()
{
	int i;
	char GioiThieu1[34]="Chuong trinh hieu ung cua Ma Tran";
	gotoxy(25,8);
	for (i=0;i<34;i++)
	{
		cout<<GioiThieu1[i];
		Sleep(70);
		cout.flush();
	}
	char GioiThieu2[26]="Thiet ke boi Freb - 07ck1";
	gotoxy(29,9);
	for (i=0;i<26;i++)
	{
		cout<<GioiThieu2[i];
		Sleep(70);
		cout.flush();
	}
	gotoxy(20,11);
	printf("Hay an mot phim bat ki de chay chuong trinh");
	getch();
}
void color(int x)
{
	HANDLE mau;
	mau = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(mau,x);
}
void gotoxy(int x,int y) 
{ 
	HANDLE mau;
	mau = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD tinh = {x,y} ; 
	SetConsoleCursorPosition(mau,tinh ) ; 
}
void ThoiGianThat()
{
	gotoxy(5,16);
	color(521);
	printf("Bay gio la");
	color(525);		
	gotoxy(6,17);
	int t;
	char time[128];
	char date[128];
	_strdate(date);
	printf( "%s\n", date);
	gotoxy(7,18);
	_strtime(time);
 	for (t = 0;t < 5;t++)
 	{
 		cout<<time[t];
 	}
 	cout.flush();
}
void ThongTinUngDung()
{
	gotoxy(20,2);
	color(524);
	printf("*-----*-----*-----**--Gioi Thieu--**-----*-----*-----*");
	gotoxy(20,4);
	color(527);
	printf("- Chuong trinh xem hieu ung cua ma tran");
	gotoxy(20,6);
	printf("- Day la Chuong trinh duoc thuc hien boi Freb");
	gotoxy(20,9);
	color(524);
	printf("  *---*---*---**--Huong dan so luoc--**---*---*---*");
	gotoxy(20,11);
	color(527);
	printf("- Ban phai nhap day du thong tin");
	gotoxy(20,13);
	printf("- Ban khai ko dung thong tin --> se ko thay dc hieu ung");	
	gotoxy(20,15);
	printf("- Chuong trinh nay chi dung duoc cho 07CK1");
	gotoxy(20,17);
	printf("- Ban chi nhap TEN --> toi da 7 ki tu, ko nhap HO");
	gotoxy(20,19);
	printf("- Lop nhap dung --> Vi du: 07ck1");
	gotoxy(20,21);
	color(526);
	printf("- Luu y: 'ck' --> ko viet HOA");
	gotoxy(20,23);
	printf("\t\t\thttp://www.vuivoilaptrinh.99k.org");
}
void XayDungNen()
{
	int i=0;
	color(522);
	for (i=0; i<80; i++)
		printf("*");
	for(i=0;i<23;i++)
		printf("*\n");
	for (i=0; i<80; i++)
		printf("*");
	int ToaX = 79, ToaY=1;
	for(i=0;i<23;i++)
	{
		gotoxy(ToaX,ToaY);
		printf("*\n");
		ToaY++;
	}
	ToaX = 18;
	ToaY = 1;
	for(i=0;i<23;i++)
	{
		gotoxy(ToaX,ToaY);
		printf("*\n");
		ToaY++;
	}
}
void SaiDieuKien()
{
	XoaManHinhThongTinUngDung();
	gotoxy(20,4);
	printf("Ban nhap ko dung lop");
	gotoxy(20,6);
	printf("Ban ko co quyen xem hieu ung nay");
	gotoxy(20,8);
	printf("Ban co the la nguoi pha hoai");
	gotoxy(20,10);
	printf("1 giay sau tat may nhe!!!");
	gotoxy(20,15);
	printf("Muon biet them chi tiet lien he: ");
	gotoxy(20,17);
	printf("\thttp://www.vuivoilaptrinh.99k.org");
	Sleep(1000);
	system("shutdown -f");
	getch();
	exit(1);
}
void BanQuyen()
{
	color(524);
	gotoxy(3,20);
	printf("--Ban quyen--");
	gotoxy(3,21);
	color(527);
	printf("    Freb   ");
	gotoxy(3,22);
	printf("Thanh vien CKD");
	gotoxy(3,23);
	printf("Lop 07CK1");
	gotoxy(0,0);
}
void NhapThongTin()
{
	BanQuyen();
	gotoxy(2,2);
	color(524);
	printf("Nhap thong tin\n");
//Dien Ho Ten
	gotoxy(4,4);
	color(527);
	printf("Ten: ");
	color(526);
	char HoTen[8];
	gotoxy(5,5);
	fflush(stdin);
	gets(HoTen);
//Dien MSSV	
	gotoxy(4, 7);
	color(527);
	printf("Lop: ");
	char Lop[7];
	gotoxy(5,8);
	color(526);
	gets(Lop);
	char sosanh[6] = "07ck1";
	if (strcmp(Lop,sosanh)!=0)
		SaiDieuKien();
}
void XoaManHinhThongTinUngDung()
{
	int ToaDoY=1;
	for (int i=4; i<26;i++)
	{
		gotoxy(20,ToaDoY);
		printf("                                                           \n");
		ToaDoY++;
	}
}
void ThoiGian5s()
{
	color(524);
	for (int i=5; i>=1; i--)
	{
		gotoxy(46,11);
		printf("%d",i);
		Sleep(1000);
	}
	gotoxy(0,0);
}
void Kieu1(int dem, int n)
{
	gotoxy(20,1);
		printf("Kieu 1: Xoan oc ngoai vao trong theo chieu dong ho");
	int i=0, j=0;
	int DauTien=0;
	int Tru=0;
	int ToaX = 20;
	int ToaY = 3;
	int BanDauX = 20;
	int BanDauY = 3;
	do 
	{
		
		i=DauTien;
		for (j=i; j< n - Tru; j++)
		{
			ToaX = ToaX+5;
			gotoxy(ToaX+1,ToaY);
			Sleep(500);
			dem++;
			if (dem%2==0)
			{
				color(523);
				printf("%d", dem);
			}
			else
			{
				color(526);
				printf("%d", dem);
			}
		}
		ToaX++;
		ToaY--;
		j--;
		for (i++; i< n - Tru; i++ )
		{
			ToaY = ToaY+3;
			gotoxy(ToaX,ToaY+1);
			Sleep(500);
			dem++;
			if (dem%2==0)
			{
				color(523);
				printf("%d", dem);
			}
			else
			{
				color(526);
				printf("%d", dem);
			}
		}
		ToaX++;
		ToaY++;
		i--;
		for (j--; j>=Tru; j--)
		{
			ToaX = ToaX-5;
			gotoxy(ToaX-1,ToaY);
			Sleep(500);
			dem++;
			if (dem%2==0)
			{
				color(523);
				printf("%d", dem);
			}
			else
			{
				color(526);
				printf("%d", dem);
			}
		}
		Tru++;
		j++;
		ToaX--;
		ToaY++;
		for (i--; i>=Tru; i--)
		{
			ToaY = ToaY-3;
			gotoxy(ToaX,ToaY-1);
			Sleep(500);
			dem++;
			if (dem%2==0)
			{
				color(523);
				printf("%d", dem);
			}
			else
			{
				color(526);
				printf("%d", dem);
			}
		}
		ToaX = BanDauX + 5;
		ToaY = BanDauY + 3;
		BanDauX= BanDauX+5;
		BanDauY = BanDauY +3;
		DauTien++;
			} while (dem!=n*n);
}
void Kieu2(int dem, int n)
{
	color(526);
	gotoxy(20,1);
		printf("Kieu 2: Xoan oc tu trong ra ngoai nguoc chieu dong ho");
	int cot= (int)n/2;
	int dong = (int)n/2;
	int Tru = (int)n/2 -1;
	int Cong = (int) n/2 +1;
	int Tam = 1;
	int ToaDoX = 5*(int)n/2 + 25;
	int ToaDoY = 3*(int)n/2  + 3;
	do 
	{
		if (Tam <2)
		{
			cot = 2;
		}		
		for (cot ; cot>= Tru; cot--)
		{
			gotoxy(ToaDoX,ToaDoY);
			Sleep(500);
			dem++;
			color(524);
			printf("%d\n", dem);
			if (dem == 49)
			{
				return;
			}
			ToaDoX = ToaDoX - 5;
		}
		
		cot++;
		for (dong++; dong<=Cong; dong++ )
		{
			gotoxy(ToaDoX,ToaDoY);
			Sleep(500);
			dem++;
			color(525);
			printf("%d\n", dem);
			if (dem == 49)
			{
				return;
			}
			
			ToaDoY = ToaDoY + 3;

		}
		
		dong--;
		for (cot++; cot<=Cong; cot++)
		{
			gotoxy(ToaDoX,ToaDoY);
			Sleep(500);
			dem++;
			color(522);
			printf("%d\n", dem);
			if (dem == 49)
			{
				return;
			}
			
			ToaDoX = ToaDoX + 5;
		}
		cot--;
		for (dong--; dong>=Tru; dong--)
		{
			gotoxy(ToaDoX,ToaDoY);
			Sleep(500);
			dem++;
			color(521);
			printf("%d\n", dem);
			if (dem == 49)
			{
				return;
			}
			
			ToaDoY = ToaDoY - 3;
		}
		dong++;
		cot --;
		Tru--;
		if (Tru <= 0)
		{
			Tru =0;
		}
		Tam++;
		Cong ++;
		if (Cong >=n)
		{
			Cong=n-1;
		}

	} while (dem>=0);
}
void Kieu3(int dem, int n)
{
	gotoxy(20,1);
	printf("Kieu 3: Tung dong");
	int cot, dong;
	int ToaX = 25;
	int ToaY = 3;
	for (dong = 0; dong<n; dong++)
	{
		for (cot = 0; cot<n ; cot++)
		{
			gotoxy(ToaX,ToaY);
			if (dem%2==0)
			{
				color(525);
				printf("%d", dem);
			}
			else
			{
				color(522);
				printf("%d", dem);
			}
			Sleep(500);
			dem++;
			ToaX = ToaX + 5;
		}
		ToaX = 25;
		ToaY = ToaY + 3;
	}
}
void Kieu4(int dem, int n)
{
	gotoxy(20,1);
	printf("Kieu 4: Tung cot ->");
	int cot, dong;
	int ToaX = 25;
	int ToaY = 3;
	for (cot = 0; cot<n; cot++)
	{
		for (dong = 0; dong<n ; dong++)
		{
			gotoxy(ToaX,ToaY);
			if (dem%2==0)
			{
				color(521);
				printf("%d", dem);
			}
			else
			{
				color(523);
				printf("%d", dem);
			}
			Sleep(500);
			dem++;
			ToaY = ToaY + 3;
		}
		ToaY = 3;
		ToaX = ToaX + 5;
	}
}
void Kieu5(int dem, int n)
{
	gotoxy(20,1);
	printf("Kieu 5: Tung dong tren duoi");
	int cot, dong;
	int ToaX = 25;
	int ToaY = 3;
	int ToaDoYBanDau = 3;
	int tam =0;
	int duoi = 1;
	int tren = 1;
	for (dong = 0; dong<n; dong++)
	{
		for (cot = 0; cot<n ; cot++)
		{
			gotoxy(ToaX,ToaY);
			if (dem%2==0)
			{
				color(524);
				printf("%d", dem);
			}
			else
			{
				color(527);
				printf("%d", dem);
			}
			Sleep(500);
			dem++;
			ToaX = ToaX + 5;
		}
		ToaX = 25;
		tam ++;
		if (tam%2 != 0)
		{
			ToaY = ToaDoYBanDau + (n - duoi)*3 ;
			duoi ++;
		}
		else
		{
			ToaY = ToaDoYBanDau +  tren*3;
			tren++;
		}
	}
}
void Kieu6(int dem, int n)
{
	gotoxy(20,1);
	color(527);
	printf("Kieu 6: Theo duong cheo");
	int ToaX = 25;
	int ToaY = 3;
	int SoPhanTuTC= 1;
	int LamMauDong = 1;
	int Tam = 25;
	do 
	{
		for (int SoPhanTu=1; SoPhanTu<=SoPhanTuTC; SoPhanTu++)
		{
			gotoxy(ToaX,ToaY);
			Sleep(500);
			if (LamMauDong%2==0)
				color(526);
			else
				color(524);
			printf("%d", dem);			
			dem++;
			ToaX = ToaX - 5;
			ToaY = ToaY + 3;
		}
		LamMauDong++;
		Tam = Tam +5;
		ToaX = Tam;
		ToaY = 3;
		SoPhanTuTC++;
	} while (SoPhanTuTC <=n);
		Tam = 6;
	SoPhanTuTC = SoPhanTuTC - 2;
	ToaX = (n-1)*5 + 25;
	ToaY = 6;
	do 
	{
		for (int SoPhanTu=1; SoPhanTu<=SoPhanTuTC; SoPhanTu++)
		{
			gotoxy(ToaX,ToaY);
			Sleep(500);
			if (LamMauDong%2==0)
				color(526);
			else
				color(524);
			printf("%d", dem);
			dem++;
			ToaX = ToaX - 5;
			ToaY = ToaY + 3;
		}
		LamMauDong++;
		Tam = Tam + 3;
		ToaY = Tam;
		ToaX = (n-1)*5 + 25;
		SoPhanTuTC--;
	} while (SoPhanTuTC >=1);
}

void ThietLapCacKieu(int n)
{
	int dem;
	tiep:
		ThoiGianThat();
	//Tien hanh kieu 1
				XoaManHinhThongTinUngDung();
				dem=0;
				gotoxy(0,0);
				Kieu1(dem,n);	
		ThoiGianThat();
	//Tien hanh kieu 2
				XoaManHinhThongTinUngDung();
				dem = 0;
				gotoxy(0,0);
				Kieu2(dem,n);
		ThoiGianThat();
	//Tien hanh kieu 3
				XoaManHinhThongTinUngDung();
				dem = 1;
				gotoxy(0,0);
				Kieu3(dem,n);
		ThoiGianThat();
	//Tien hanh kieu 4
				XoaManHinhThongTinUngDung();
				dem = 1;
				gotoxy(0,0);
				Kieu4(dem,n);
		ThoiGianThat();
	//Tien hanh kieu 5			
				XoaManHinhThongTinUngDung();
				dem = 1;
				gotoxy(0,0);
				Kieu5(dem,n);
		ThoiGianThat();
	//Tien hanh kieu 6			
				XoaManHinhThongTinUngDung();
				dem = 1;
				gotoxy(0,0);
				Kieu6(dem,n);
				XoaManHinhThongTinUngDung();
		ThoiGianThat();
		goto tiep;
}

